<?php
namespace zhtfrontend\modules\cus\controllers;

use common\components\helpers\Fun;
use common\models\datas\CustomerUpgradeRule;
use yii\data\Pagination;
use zhtfrontend\components\bases\BaseController;


class CustomerupgraderuleController extends BaseController
{
    public function actionIndex()
    {
        $where = \Yii::$app->request->get();
        $model = CustomerUpgradeRule::find();
        if(!empty($where['grade_id'])){
            $model->andWhere(['grade_id'=>$where['grade_id']]);
        }
        $pages = new Pagination(['totalCount' => $model->count(), 'pageSize' => 12]);
        isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
        $data = $model->offset($pages->offset)->limit($pages->limit)->all();
        $data = Fun::asArray($data,['gradename','operate']);
        $gradeList = [];
        return $this->render('index', ['data' => $data,'gradeList'=>$gradeList,'pages'=>$pages]);
    }

    //针对类型加以区分
    public function actionCreate()
    {
        $this->layout = '@app/views/layouts/iframe.php';
        $model = new CustomerUpgradeRule();
        if($model->load(\Yii::$app->request->post()) && $model->validate()){
             if($model->save(false)){
                 return $this->redirectMessage('添加成功',Fun::url(['cus/customerupgraderule/index']));
             }
        }
        $gradeList = [];
        return $this->render('create', ['model' => $model]);
    }

    public function actionUpdate()
    {
        $this->layout = '@app/views/layouts/iframe.php';
        $id = \Yii::$app->request->get('id');
        $model = CustomerUpgradeRule::findOne($id);
        if($model->load(\Yii::$app->request->post()) && $model->validate()){
           if($model->save(false)){
               return $this->redirectMessage('修改成功',Fun::url(['cus/customerupgraderule/index']));
           }
        }
        $gradeList = [];
        return $this->render('create', ['model' => $model]);
    }

}
